package com.like.adapter;

import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.like.entity.EventWrapper;
import com.like.entity.InfoListResp;
import com.like.entity.M;
import com.like.fragment.MainDocumentFragment;
import com.like.hospitalworld.DocumentDetailActivity;
import com.like.hospitalworld.R;
import com.like.utils.ImageUtils;

import org.greenrobot.eventbus.EventBus;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class DocumentAdapter extends BaseAdapter {

    private M m;
    private List<InfoListResp.InformationBase> mInfos;

    public DocumentAdapter(List<InfoListResp.InformationBase> infos, M m) {
        this.m = m;
        this.mInfos = infos;
        if (infos == null)
            this.mInfos = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return mInfos.size();
    }

    @Override
    public Object getItem(int position) {
        return mInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh;
        Object obj = getItem(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_document,
                    parent, false);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        vh.setTags(position);
        InfoListResp.InformationBase info = (InfoListResp.InformationBase) obj;
        vh.dTitle.setText(info.titleName);
        ImageUtils.showImage(m.mContext, info.titlePage, m.getToken(), vh.sUserIcon);
        if (TextUtils.isEmpty(info.summary) || TextUtils.equals("null", info.summary))
            info.summary = "";
        vh.dArticle.setText(info.summary);
        return convertView;
    }

    public class ViewHolder {
        @ViewInject(R.id.activity_iv)
        ImageView sUserIcon;
        @ViewInject(R.id.item_document_title)
        public TextView dTitle;
        @ViewInject(R.id.item_document_article)
        public TextView dArticle;
        @ViewInject(R.id.read_all)
        Button mReadAll;
        @ViewInject(R.id.download)
        Button mDownload;

        public void setTags(int position) {
            mReadAll.setTag(position);
            mDownload.setTag(position);
        }

        @Event(R.id.read_all)
        private void readAllClick(View v) {
            int position = (int) v.getTag();
            EventBus.getDefault().postSticky(new EventWrapper(mInfos.get(position),
                    DocumentDetailActivity.class,
                    DocumentDetailActivity.REQUEST_INFO_BASE));
            Intent intent = new Intent(m.mContext, DocumentDetailActivity.class);
            m.mContext.startActivity(intent);
        }

        @Event(R.id.download)
        private void downloadClick(View v) {
            int position = (int) v.getTag();
            InfoListResp.InformationBase info = (InfoListResp.InformationBase) getItem(position);
            EventBus.getDefault().post(new EventWrapper(info, MainDocumentFragment.class, MainDocumentFragment.REQUEST_COURSE_DOWNLOAD));
        }

        public ViewHolder(View convertView) {
            x.view().inject(this, convertView);
        }
    }

    public void update(List<InfoListResp.InformationBase> infos, boolean append) {
        if (!append)
            this.mInfos = infos;
        else
            this.mInfos.addAll(infos);
        this.notifyDataSetChanged();
    }
}
